<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\Coupon;
use \App\Models\Merchant;

class CouponsController extends AdminController
{
    protected $title = '优惠卷管理';
    
    protected function grid()
    {
        $grid = new Grid(new Coupon());

	    $grid->disableExport();
	
	    $grid->column('id', 'ID');
	    $grid->column('name', '卡卷名称');
	    $grid->column('type', '卡卷类型')->display(function ($value) {
		    return config('site.couponType')[$value];
	    });
	    $grid->column('scope', '卡卷范围')->display(function ($value) {
		    return  !empty($value) ? config('site.couponScope')[$value] : '';
	    });
	    $grid->column('func', '卡卷优惠方式')->display(function ($value) {
		      return  !empty($value) ? config('site.couponFunc')[$value] : '';
	    });
	    $grid->column('value', '优惠金额或百分比');
	    $grid->column('min_amount', '最低消费金额');
	    $grid->column('total', '发放总数');
	     $grid->column('hours', '减免时间');

	    $grid->column('enabled', '启用状态')->bool();
	    $grid->column('start_time', '生效时间')->display(function () {
	    	if($this->is_date == 1){
	    		return $this->start_time;
	    	}else{

	    		return $this->no_date_started_at;
	    	}
		    
	    });
	    $grid->column('end_time', '过期时间')->display(function () {
		    //return $this->end_time;

		    if($this->is_date == 1){
	    		return $this->end_time;
	    	}else{

	    		return $this->no_date_end_at;
	    	}
	    });
	    $grid->column('created_at', '创建时间')->display(function () {
		    return $this->created_at->toDatetimeString();
	    });
	
	    $grid->disableRowSelector();

        return $grid;
    }
	
	 protected function detail($id)
    {
        $show = new Show(Coupon::findOrFail($id));
        return $show;
    }
	
    protected function form()
    {
        $form = new Form(new Coupon());

	    $form->text('name', '卡卷名称')->rules('required');
	    $form->select('type', '卡卷类型')->options(config('site.couponType'))->rules('required')->when("park", function (Form $form) { 

	        $form->number('hours','减免时间');

	    })->when("coupon", function (Form $form) { 

	         $form->select('scope', '卡卷范围')->options(config('site.couponScope'))->when("shop", function (Form $form) { 

		        $form->multipleSelect('merchant_ids','关联商户')->options(Merchant::all()->pluck('name', 'id'));

		    });
		    $form->select('func', '卡卷优惠方式')->options(config('site.couponFunc'));
		    

		    $form->currency('value', '优惠金额(金额或百分比)百分比填80就是百分之80%')
				    ->symbol('￥')
				    ->rules('numeric');
			$form->currency('min_amount', '最低消费金额')
				    ->symbol('￥')
				    ->rules('numeric');

	    })->when("admission", function (Form $form) { 

	         $form->select('scope', '卡卷范围')->options(config('site.couponScope'))->when("shop", function (Form $form) { 

		        $form->multipleSelect('merchant_ids','关联商户')->options(Merchant::all()->pluck('name', 'id'));

		    });
		    

	    });
	    $form->number('total', '发放卡卷总数')->rules('numeric');
	     $form->radio('is_date', '日期卡券生效')->options(config('site.bool'))->when("0", function (Form $form) { 

		       $form->timeRange('no_date_started_at', 'no_date_end_at', '卡卷生效时间范围');

		    })->when("1", function (Form $form) { 

		       $form->dateTimeRange('start_time', 'end_time', '卡卷生效时间范围');

		    });   
	    

	    $form->radio('enabled', '是否启用')->default(true)->options(config('site.bool'));
        return $form;
    }
}
